

<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
  <meta charset="utf-8" />
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  
  <title>mindspore.ops.composite.clip_ops &mdash; MindSpore master documentation</title>
  

  
  <link rel="stylesheet" href="../../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />

  
  

  
  

  

  
  <!--[if lt IE 9]>
    <script src="../../../../_static/js/html5shiv.min.js"></script>
  <![endif]-->
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../../../" src="../../../../_static/documentation_options.js"></script>
        <script src="../../../../_static/jquery.js"></script>
        <script src="../../../../_static/underscore.js"></script>
        <script src="../../../../_static/doctools.js"></script>
        <script src="../../../../_static/language_data.js"></script>
        <script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    
    <script type="text/javascript" src="../../../../_static/js/theme.js"></script>

    
    <link rel="index" title="Index" href="../../../../genindex.html" />
    <link rel="search" title="Search" href="../../../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../../../index.html" class="icon icon-home"> MindSpore
          

          
          </a>

          
            
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        
        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">MindSpore Python API</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.html">mindspore</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.common.initializer.html">mindspore.common.initializer</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.communication.html">mindspore.communication</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.compression.html">mindspore.compression</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.context.html">mindspore.context</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.dataset.html">mindspore.dataset</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.dataset.audio.html">mindspore.dataset.audio</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.dataset.config.html">mindspore.dataset.config</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.dataset.text.html">mindspore.dataset.text</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.dataset.transforms.html">mindspore.dataset.transforms</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.dataset.vision.html">mindspore.dataset.vision</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.mindrecord.html">mindspore.mindrecord</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.nn.html">mindspore.nn</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.nn.probability.html">mindspore.nn.probability</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.nn.transformer.html">mindspore.nn.transformer</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.numpy.html">mindspore.numpy</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.ops.html">mindspore.ops</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.parallel.html">mindspore.parallel</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.parallel.nn.html">mindspore.parallel.nn</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.profiler.html">mindspore.profiler</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.scipy.html">mindspore.scipy</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.train.html">mindspore.train</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.boost.html">mindspore.boost</a></li>
</ul>
<p class="caption"><span class="caption-text">MindSpore C++ API</span></p>
<ul>
<li class="toctree-l1"><a class="reference external" href="https://www.mindspore.cn/lite/api/zh-CN/master/api_cpp/mindspore.html">MindSpore Lite↗</a></li>
</ul>

            
          
        </div>
        
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../../index.html">MindSpore</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          

















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../../index.html" class="icon icon-home"></a> &raquo;</li>
        
          <li><a href="../../../index.html">Module code</a> &raquo;</li>
        
      <li>mindspore.ops.composite.clip_ops</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for mindspore.ops.composite.clip_ops</h1><div class="highlight"><pre>
<span></span><span class="c1"># Copyright 2020 Huawei Technologies Co., Ltd</span>
<span class="c1">#</span>
<span class="c1"># Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);</span>
<span class="c1"># you may not use this file except in compliance with the License.</span>
<span class="c1"># You may obtain a copy of the License at</span>
<span class="c1">#</span>
<span class="c1"># http://www.apache.org/licenses/LICENSE-2.0</span>
<span class="c1">#</span>
<span class="c1"># Unless required by applicable law or agreed to in writing, software</span>
<span class="c1"># distributed under the License is distributed on an &quot;AS IS&quot; BASIS,</span>
<span class="c1"># WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span>
<span class="c1"># See the License for the specific language governing permissions and</span>
<span class="c1"># limitations under the License.</span>
<span class="c1"># ============================================================================</span>

<span class="sd">&quot;&quot;&quot;Operations for clipping tensors to min/max values.&quot;&quot;&quot;</span>
<span class="kn">from</span> <span class="nn">mindspore.nn.cell</span> <span class="kn">import</span> <span class="n">Cell</span>
<span class="kn">from</span> <span class="nn">mindspore.ops</span> <span class="kn">import</span> <span class="n">composite</span> <span class="k">as</span> <span class="n">C</span>
<span class="kn">from</span> <span class="nn">mindspore.ops</span> <span class="kn">import</span> <span class="n">functional</span> <span class="k">as</span> <span class="n">F</span>
<span class="kn">from</span> <span class="nn">mindspore.ops</span> <span class="kn">import</span> <span class="n">operations</span> <span class="k">as</span> <span class="n">P</span>
<span class="kn">from</span> <span class="nn">mindspore.common.tensor</span> <span class="kn">import</span> <span class="n">Tensor</span>
<span class="kn">from</span> <span class="nn">mindspore.common</span> <span class="kn">import</span> <span class="n">dtype</span> <span class="k">as</span> <span class="n">mstype</span>
<span class="kn">from</span> <span class="nn">mindspore._checkparam</span> <span class="kn">import</span> <span class="n">Rel</span>
<span class="kn">from</span> <span class="nn">mindspore._checkparam</span> <span class="kn">import</span> <span class="n">Validator</span> <span class="k">as</span> <span class="n">validator</span>
<span class="kn">from</span> <span class="nn">mindspore.ops.primitive</span> <span class="kn">import</span> <span class="n">constexpr</span>


<span class="nd">@constexpr</span>
<span class="k">def</span> <span class="nf">_check_shape</span><span class="p">(</span><span class="n">input_shape</span><span class="p">,</span> <span class="n">out_shape</span><span class="p">,</span> <span class="n">prim_name</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="n">msg_prefix</span> <span class="o">=</span> <span class="sa">f</span><span class="s2">&quot;For &#39;</span><span class="si">{</span><span class="n">prim_name</span><span class="si">}</span><span class="s2">&#39;, the&quot;</span> <span class="k">if</span> <span class="n">prim_name</span> <span class="k">else</span> <span class="s2">&quot;The&quot;</span>
    <span class="k">if</span> <span class="n">input_shape</span> <span class="o">!=</span> <span class="n">out_shape</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">msg_prefix</span><span class="si">}</span><span class="s2"> input &#39;x&#39; shape should be equal to the output shape, but got &quot;</span>
                         <span class="sa">f</span><span class="s2">&quot;input &#39;x&#39; shape </span><span class="si">{</span><span class="n">input_shape</span><span class="si">}</span><span class="s2">, output shape </span><span class="si">{</span><span class="n">out_shape</span><span class="si">}</span><span class="s2">.&quot;</span><span class="p">)</span>


<div class="viewcode-block" id="clip_by_value"><a class="viewcode-back" href="../../../../api_python/ops/mindspore.ops.clip_by_value.html#mindspore.ops.clip_by_value">[docs]</a><span class="k">def</span> <span class="nf">clip_by_value</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">clip_value_min</span><span class="p">,</span> <span class="n">clip_value_max</span><span class="p">):</span>
    <span class="sa">r</span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Clips tensor values to a specified min and max.</span>

<span class="sd">    Limits the value of :math:`x` to a range, whose lower limit is &#39;clip_value_min&#39;</span>
<span class="sd">    and upper limit is &#39;clip_value_max&#39;.</span>

<span class="sd">    .. math::</span>

<span class="sd">        out_i= \left\{</span>
<span class="sd">        \begin{array}{align}</span>
<span class="sd">            clip\_value_{max} &amp; \text{ if } x_i\ge  clip\_value_{max} \\</span>
<span class="sd">            x_i &amp; \text{ if } clip\_value_{min} \lt x_i \lt clip\_value_{max} \\</span>
<span class="sd">            clip\_value_{min} &amp; \text{ if } x_i \le clip\_value_{min} \\</span>
<span class="sd">        \end{array}\right.</span>

<span class="sd">    Note:</span>
<span class="sd">        &#39;clip_value_min&#39; needs to be less than or equal to &#39;clip_value_max&#39;.</span>

<span class="sd">    Args:</span>
<span class="sd">          x (Tensor): Input data. The shape is :math:`(N,*)` where :math:`*` means, any number of additional dimensions.</span>
<span class="sd">          clip_value_min (Tensor): The minimum value.</span>
<span class="sd">          clip_value_max (Tensor): The maximum value.</span>

<span class="sd">    Returns:</span>
<span class="sd">          Tensor, a clipped Tensor. It has the same shape and data type as `x`.</span>

<span class="sd">    Supported Platforms:</span>
<span class="sd">        ``Ascend`` ``GPU`` ``CPU``</span>

<span class="sd">    Examples:</span>
<span class="sd">        &gt;&gt;&gt; from mindspore import Tensor, ops</span>
<span class="sd">        &gt;&gt;&gt; import numpy as np</span>
<span class="sd">        &gt;&gt;&gt; min_value = Tensor(5, mindspore.float32)</span>
<span class="sd">        &gt;&gt;&gt; max_value = Tensor(20, mindspore.float32)</span>
<span class="sd">        &gt;&gt;&gt; x = Tensor(np.array([[1., 25., 5., 7.], [4., 11., 6., 21.]]), mindspore.float32)</span>
<span class="sd">        &gt;&gt;&gt; output = ops.clip_by_value(x, min_value, max_value)</span>
<span class="sd">        &gt;&gt;&gt; print(output)</span>
<span class="sd">        [[ 5. 20.  5.  7.]</span>
<span class="sd">         [ 5. 11.  6. 20.]]</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">min_op</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Minimum</span><span class="p">()</span>
    <span class="n">max_op</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Maximum</span><span class="p">()</span>
    <span class="n">x_min</span> <span class="o">=</span> <span class="n">min_op</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">clip_value_max</span><span class="p">)</span>
    <span class="n">x_max</span> <span class="o">=</span> <span class="n">max_op</span><span class="p">(</span><span class="n">x_min</span><span class="p">,</span> <span class="n">clip_value_min</span><span class="p">)</span>
    <span class="n">_check_shape</span><span class="p">(</span><span class="n">F</span><span class="o">.</span><span class="n">shape</span><span class="p">(</span><span class="n">x</span><span class="p">),</span> <span class="n">F</span><span class="o">.</span><span class="n">shape</span><span class="p">(</span><span class="n">x_max</span><span class="p">),</span> <span class="s1">&#39;clip_by_value&#39;</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">x_max</span></div>


<span class="n">get_square_sum</span> <span class="o">=</span> <span class="n">C</span><span class="o">.</span><span class="n">MultitypeFuncGraph</span><span class="p">(</span><span class="s2">&quot;get_square_sum&quot;</span><span class="p">)</span>
<span class="nd">@get_square_sum</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="s2">&quot;Tensor&quot;</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">_get_square_sum</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
    <span class="n">norm</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">ReduceSum</span><span class="p">(</span><span class="kc">False</span><span class="p">)(</span><span class="n">F</span><span class="o">.</span><span class="n">square</span><span class="p">(</span><span class="n">x</span><span class="p">),</span> <span class="p">())</span>
    <span class="n">norm</span> <span class="o">=</span> <span class="n">F</span><span class="o">.</span><span class="n">expand_dims</span><span class="p">(</span><span class="n">F</span><span class="o">.</span><span class="n">cast</span><span class="p">(</span><span class="n">norm</span><span class="p">,</span> <span class="n">mstype</span><span class="o">.</span><span class="n">float32</span><span class="p">),</span> <span class="mi">0</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">norm</span>


<span class="n">apply_global_norm</span> <span class="o">=</span> <span class="n">C</span><span class="o">.</span><span class="n">MultitypeFuncGraph</span><span class="p">(</span><span class="s2">&quot;apply_global_norm&quot;</span><span class="p">)</span>
<span class="nd">@apply_global_norm</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="s2">&quot;Tensor&quot;</span><span class="p">,</span> <span class="s2">&quot;Tensor&quot;</span><span class="p">,</span> <span class="s2">&quot;Tensor&quot;</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">_apply_global_norm</span><span class="p">(</span><span class="n">clip_norm</span><span class="p">,</span> <span class="n">global_norm</span><span class="p">,</span> <span class="n">x</span><span class="p">):</span>
    <span class="n">x_dtype</span> <span class="o">=</span> <span class="n">F</span><span class="o">.</span><span class="n">dtype</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
    <span class="n">x</span> <span class="o">=</span> <span class="n">x</span> <span class="o">*</span> <span class="n">clip_norm</span> <span class="o">/</span> <span class="n">global_norm</span>
    <span class="n">x</span> <span class="o">=</span> <span class="n">F</span><span class="o">.</span><span class="n">cast</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">x_dtype</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">x</span>


<span class="k">class</span> <span class="nc">_ClipByGlobalNorm</span><span class="p">(</span><span class="n">Cell</span><span class="p">):</span>
    <span class="sa">r</span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Clips tensor values by the ratio of the sum of their norms.</span>

<span class="sd">    Args:</span>
<span class="sd">        clip_norm (Union(float, int)): The clipping ratio. Default: 1.0</span>
<span class="sd">        use_norm (Union(float, None)): The global norm. Default: None</span>

<span class="sd">    Inputs:</span>
<span class="sd">        - **x** (Union(tuple[Tensor], list[Tensor])) - Input data to clip.</span>

<span class="sd">    Outputs:</span>
<span class="sd">        Tensor, a clipped Tensor.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">clip_norm</span><span class="o">=</span><span class="mf">1.0</span><span class="p">,</span> <span class="n">use_norm</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Initialize _ClipByGlobalNorm.&quot;&quot;&quot;</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">_ClipByGlobalNorm</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
        <span class="c1"># Add interface. This parameter is not used at present</span>
        <span class="k">if</span> <span class="n">use_norm</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;For &#39;</span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">cls_name</span><span class="si">}</span><span class="s2">&#39;, input &#39;use_norm&#39; only supports None currently, &quot;</span>
                             <span class="sa">f</span><span class="s2">&quot;but got &#39;use_norm&#39;: </span><span class="si">{</span><span class="n">use_norm</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">)</span>
        <span class="n">validator</span><span class="o">.</span><span class="n">check_number</span><span class="p">(</span><span class="s2">&quot;clip_norm&quot;</span><span class="p">,</span> <span class="n">clip_norm</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">,</span> <span class="n">Rel</span><span class="o">.</span><span class="n">GT</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">cls_name</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">clip_norm</span> <span class="o">=</span> <span class="n">Tensor</span><span class="p">([</span><span class="n">clip_norm</span><span class="p">],</span> <span class="n">mstype</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">hyper_map</span> <span class="o">=</span> <span class="n">C</span><span class="o">.</span><span class="n">HyperMap</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">greater_equal</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">GreaterEqual</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">construct</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">):</span>
        <span class="n">square_sum</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">hyper_map</span><span class="p">(</span><span class="n">get_square_sum</span><span class="p">,</span> <span class="n">x</span><span class="p">)</span>
        <span class="n">global_norm</span> <span class="o">=</span> <span class="n">F</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">F</span><span class="o">.</span><span class="n">addn</span><span class="p">(</span><span class="n">square_sum</span><span class="p">))</span>
        <span class="n">cond</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">greater_equal</span><span class="p">(</span><span class="n">global_norm</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">clip_norm</span><span class="p">)</span>
        <span class="n">global_norm</span> <span class="o">=</span> <span class="n">F</span><span class="o">.</span><span class="n">select</span><span class="p">(</span><span class="n">cond</span><span class="p">,</span> <span class="n">global_norm</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">clip_norm</span><span class="p">)</span>
        <span class="n">clip_x</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">hyper_map</span><span class="p">(</span><span class="n">F</span><span class="o">.</span><span class="n">partial</span><span class="p">(</span><span class="n">apply_global_norm</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">clip_norm</span><span class="p">,</span> <span class="n">global_norm</span><span class="p">),</span> <span class="n">x</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">clip_x</span>


<span class="nd">@constexpr</span>
<span class="k">def</span> <span class="nf">_check_value</span><span class="p">(</span><span class="n">clip_norm</span><span class="p">):</span>
    <span class="n">validator</span><span class="o">.</span><span class="n">check_number</span><span class="p">(</span><span class="s2">&quot;clip_norm&quot;</span><span class="p">,</span> <span class="n">clip_norm</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">,</span> <span class="n">Rel</span><span class="o">.</span><span class="n">GT</span><span class="p">,</span> <span class="s2">&quot;clip_by_global_norm&quot;</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">clip_norm</span>


<div class="viewcode-block" id="clip_by_global_norm"><a class="viewcode-back" href="../../../../api_python/ops/mindspore.ops.clip_by_global_norm.html#mindspore.ops.clip_by_global_norm">[docs]</a><span class="k">def</span> <span class="nf">clip_by_global_norm</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">clip_norm</span><span class="o">=</span><span class="mf">1.0</span><span class="p">,</span> <span class="n">use_norm</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="sa">r</span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Clips tensor values by the ratio of the sum of their norms.</span>

<span class="sd">    Note:</span>
<span class="sd">        Input &#39;x&#39; should be a tuple or list of tensors. Otherwise, it will raise an error.</span>

<span class="sd">    Args:</span>
<span class="sd">        x (Union(tuple[Tensor], list[Tensor])): Input data to clip.</span>
<span class="sd">          The shape of each Tensor in tuple is :math:`(N,*)` where :math:`*` means,</span>
<span class="sd">          any number of additional dimensions.</span>
<span class="sd">        clip_norm (Union(float, int)): The clipping ratio, it should be greater than 0. Default: 1.0</span>
<span class="sd">        use_norm (None): The global norm. Default: None. Currently only none is supported.</span>

<span class="sd">    Returns:</span>
<span class="sd">        tuple[Tensor], a clipped Tensor. It has the same data type as `x` and each Tensor in the output tuple is the</span>
<span class="sd">        same as the original input shape.</span>

<span class="sd">    Supported Platforms:</span>
<span class="sd">        ``Ascend`` ``GPU`` ``CPU``</span>

<span class="sd">    Examples:</span>
<span class="sd">        &gt;&gt;&gt; from mindspore import Tensor, ops</span>
<span class="sd">        &gt;&gt;&gt; import numpy as np</span>
<span class="sd">        &gt;&gt;&gt; x1 = np.array([[2., 3.], [1., 2.]]).astype(np.float32)</span>
<span class="sd">        &gt;&gt;&gt; x2 = np.array([[1., 4.], [3., 1.]]).astype(np.float32)</span>
<span class="sd">        &gt;&gt;&gt; input_x = (Tensor(x1), Tensor(x2))</span>
<span class="sd">        &gt;&gt;&gt; out = ops.clip_by_global_norm(input_x, 1.0)</span>
<span class="sd">        &gt;&gt;&gt; print(out)</span>
<span class="sd">        (Tensor(shape=[2, 2], dtype=Float32, value=</span>
<span class="sd">        [[ 2.98142403e-01,  4.47213590e-01],</span>
<span class="sd">         [ 1.49071202e-01,  2.98142403e-01]]), Tensor(shape=[2, 2], dtype=Float32, value=</span>
<span class="sd">        [[ 1.49071202e-01,  5.96284807e-01],</span>
<span class="sd">         [ 4.47213590e-01,  1.49071202e-01]]))</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">clip_norm</span> <span class="o">=</span> <span class="n">_check_value</span><span class="p">(</span><span class="n">clip_norm</span><span class="p">)</span>
    <span class="n">clip_val</span> <span class="o">=</span> <span class="n">_ClipByGlobalNorm</span><span class="p">(</span><span class="n">clip_norm</span><span class="p">,</span> <span class="n">use_norm</span><span class="p">)(</span><span class="n">x</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">clip_val</span></div>
</pre></div>

           </div>
           
          </div>
          <footer>

  <hr/>

  <div role="contentinfo">
    <p>
        &#169; Copyright 2021, MindSpore.

    </p>
  </div>
    
    
    
    Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
    
    <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
    
    provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>
        </div>
      </div>

    </section>

  </div>
  

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>